//
//  BZNetworkTools.swift
//  weibo
//
//  Created by zzx on 2017/7/30.
//  Copyright © 2017年 zzx. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType {
    case get
    case post
}

class BZNetworkTools: AFHTTPSessionManager {
    
    //单例全局访问点
    static let sharedTools: BZNetworkTools = {
        
        let tools = BZNetworkTools()
        
        //添加可接受的服务端
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
        
    }()
    
    
    //MARK:- 封装get和post请求
    fileprivate func requset(type: RequestType, urlStr: String, pramaters: Any?, finishCallBack:@escaping (Any?, Error?) -> ()){
    
        
        if type == .get {
            
            self.get(urlStr, parameters: pramaters, progress: nil, success: { (_, response: Any?) in
                
                finishCallBack(response, nil)
                
            }, failure: { (_, error) in
                
                finishCallBack(nil, error)
                
            })
            
            
        }else{
            
            self.post(urlStr, parameters: pramaters, progress: nil, success: { (_, response: Any?) in
                
                finishCallBack(response, nil)
                
            }, failure: { (_, error) in
                
                finishCallBack(nil, error)
                
            })
            
            
        }
        
    
    }
    
   
    
    
    
}

//MARK:- 首页相关接口
extension BZNetworkTools{
    //获取登陆及其所关注的微博用户数据
    func requestStatuses(access_token: String, callBack: @escaping (Any?, Error?)->()) {
    
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        //接口参数
        let params = [
            
            "access_token" : access_token
            
        ]
        
        requset(type: .get, urlStr: urlStr, pramaters: params, finishCallBack: callBack)
        
        
    }
    
    
    
    
}


//MARK:- OAuth授权相关接口
extension BZNetworkTools{

    //获取用户信息
    func requestUserInfo(accessToken:String, uid: Int64,callBack: @escaping (Any?, Error?)->() ) {
        
        let urlStr = "https://api.weibo.com/2/users/show.json"
        
        //接口参数
        let params: [String: Any] = [
            
            "access_token": accessToken,
            "uid": uid
            
        ]
        
        //网络加载请求
        requset(type: .get, urlStr: urlStr, pramaters: params, finishCallBack: callBack)
        
        
    }

    
    //获取令牌
    func requestAccessToken(code: String , callBack: @escaping (Any?, Error?)->()) {
        
        //获取接口地址
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        
        //接口参数
        let params = [
            
            "client_id": AppKey,
            "client_secret": AppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": Redirect_uri
            
        ]
        
        requset(type: .post, urlStr: urlStr, pramaters: params, finishCallBack: callBack)
        
        
    }


}
